//
//  BaseNavigationController.m
//  Postre
//
//  Created by CoderLT on 2017/5/22.
//  Copyright © 2017年 CoderLT. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UINavigationControllerDelegate>
{
    
}
@property (nonatomic, weak) UIViewController *pushingVC;
@end

@implementation BaseNavigationController
+ (void)initialize
{
    // 设置导航栏背景颜色\文字颜色
    UINavigationBar *navBar = [UINavigationBar appearance];
    navBar.barStyle = UIBarStyleDefault;
    navBar.tintColor = [UIColor appNavTitleColor];
    navBar.barTintColor = [UIColor appNavBgColor];
    [navBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor appNavTitleColor], NSFontAttributeName : [UIFont appHeaderFont]}];
    
    // 设置按钮文字颜色
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    [item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor appNavItemTitleColor], NSFontAttributeName : [UIFont appNavTitleFont]}
                        forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [[UIColor appNavItemTitleColor] colorWithAlphaComponent:0.7f], NSFontAttributeName : [UIFont appNavTitleFont]}
                        forState:UIControlStateHighlighted];
    [item setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor appNavItemTitleDisableColor], NSFontAttributeName : [UIFont appNavTitleFont]}
                        forState:UIControlStateDisabled];
    
    [navBar setBackgroundImage:[UIImage imageWithColor:[UIColor appNavBgColor]]
                forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [navBar setShadowImage:[UIImage imageWithColor:[UIColor appNavSepLineColor] size:CGSizeMake(1, 0.5f)]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate = self;
}

#pragma mark - tabbar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (!viewController || self.topViewController == viewController || (self.pushingVC && self.pushingVC != viewController)) {
        NSLog(@" ------- 被拦截 -------");
        return;
    }
    self.pushingVC = viewController;
    if (self.viewControllers.count && !viewController.at_showBottomBarWhenPushed) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated {
    [viewControllers enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) {
        obj.hidesBottomBarWhenPushed = (idx != 0 && !obj.at_showBottomBarWhenPushed);
    }];
    [super setViewControllers:viewControllers animated:animated];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.pushingVC = nil;
}

#pragma mark - autorotate
- (UIStatusBarStyle)preferredStatusBarStyle {
    return [[self.viewControllers lastObject] preferredStatusBarStyle];
}
- (BOOL)prefersStatusBarHidden {
    return [[self.viewControllers lastObject] prefersStatusBarHidden];
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return [[self.viewControllers lastObject] preferredStatusBarUpdateAnimation];
}
- (BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
